package com.andova.image

import android.app.Application
import android.view.MotionEvent
import android.view.View
import androidx.test.core.app.ActivityScenario
import androidx.test.core.app.ApplicationProvider
import androidx.test.espresso.Espresso
import androidx.test.espresso.UiController
import androidx.test.espresso.ViewAction
import androidx.test.espresso.matcher.ViewMatchers
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.andova.gltest.GlCaptureViewActivity
import com.andova.opengles.R
import org.hamcrest.Matcher
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class CaptureActivityTest {
    @Test
    fun testCapture() {
        val a = ApplicationProvider.getApplicationContext<Application>()
        ActivityScenario.launch(GlCaptureViewActivity::class.java)
        Thread.sleep(1000L)
        for (i in 0..1) {
            Espresso.onView(ViewMatchers.withId(R.id.btn_capture)).perform(object : ViewAction {
                override fun getConstraints(): Matcher<View> = ViewMatchers.isDisplayed()
                override fun getDescription(): String = ""
                override fun perform(uiController: UiController?, view: View?) {
                    view?.onTouchEvent(MotionEvent.obtain(5L, 10L, MotionEvent.ACTION_DOWN, 0f, 0f, 0))
                    view?.onTouchEvent(MotionEvent.obtain(5L, 10L, MotionEvent.ACTION_UP, 0f, 0f, 0))
                }
            })
            Thread.sleep(1000L)
            Espresso.onView(ViewMatchers.withId(R.id.btn_cancel)).perform(object : ViewAction {
                override fun getConstraints(): Matcher<View> = ViewMatchers.isClickable()
                override fun getDescription(): String = ""
                override fun perform(uiController: UiController?, view: View?) {
                    view?.performClick()
                }
            })
            Thread.sleep(2000L)
        }
    }
}